package com.woniuxy.goods.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniuxy.core.util.StateNum;
import com.woniuxy.goods.dto.AttrGroupDto;
import com.woniuxy.goods.dto.GoodsCatDto;
import com.woniuxy.goods.dto.GroupTypeDto;
import com.woniuxy.goods.entity.AttributeGroup;
import com.woniuxy.goods.entity.GoodsBrandCat;
import com.woniuxy.goods.entity.GoodsCat;
import com.woniuxy.goods.entity.GroupType;
import com.woniuxy.goods.mapper.GroupTypeMapper;
import com.woniuxy.goods.param.AddGroupTypeParam;
import com.woniuxy.goods.param.UpdateGroupTypeParam;
import com.woniuxy.goods.service.AttributeConfigService;
import com.woniuxy.goods.service.AttributeGroupService;
import com.woniuxy.goods.service.GoodsCatService;
import com.woniuxy.goods.service.GroupTypeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.web.util.ConvertUtil;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 * 类别、属性组绑定服务层实现类
 * @author ...
 * @since 2024年05月22日
 */
@Service
public class GroupTypeServiceImpl extends ServiceImpl<GroupTypeMapper, GroupType> implements GroupTypeService {

    /**
     * 类别管理服务层接口
     */
    @Resource
    private GoodsCatService goodsCatService;
    /**
     * 属性组管理服务层接口
     */
    @Resource
    private AttributeGroupService groupService;

    /**
     * 新增属性组、类别绑定
     * @param param 新增类别属性组绑定对象
     * @return 类别属性组绑定dto对象集合
     * @throws Exception
     */
    @Override
    @GlobalTransactional(rollbackFor = Throwable.class)
    public List<GroupTypeDto> addGroupType(AddGroupTypeParam param) throws Exception {
        GoodsCat goodsCat = goodsCatService.findCatById(param.getCatId());
        if (goodsCat==null||goodsCat.getUpCatId()==0) throw new Exception("所选物品类别存在异常");
        List<AttrGroupDto> groupList = groupService.findAttrGroupByIds(param.getGroupIds());
        if (groupList==null||groupList.size()!=param.getGroupIds().size()) throw new Exception("所选属性组存在异常");
        QueryWrapper<GroupType> wrapper = new QueryWrapper<>();
        wrapper.in("group_id",param.getGroupIds());
        wrapper.eq("cat_id",param.getCatId());
        GroupType query = getOne(wrapper);
        if (ObjectUtil.isNotEmpty(query)) throw new Exception("已存在对应品牌、类别绑定信息");
        List<GroupTypeDto> dtos = new ArrayList<>();
        for (AttrGroupDto groupDto : groupList) {
            GroupType groupType = new GroupType();
            groupType.setCatId(param.getCatId());
            groupType.setGroupId(groupDto.getGroupId());
            save(groupType);
            GroupTypeDto dto = BeanUtil.toBean(groupType, GroupTypeDto.class);
            dto.setGroupDto(groupDto);
            dtos.add(dto);
        }
        return dtos;
    }

    /**
     * 通过id删除属性组、类别绑定
     * @param id 类别、属性组绑定信息id
     * @return
     * @throws Exception
     */
    @Override
    public void delGroupTypeById(Integer id) {
        removeById(id);
    }

    /**
     * 通过属性组id删除属性组、类别绑定
     * @param groupId 属性组id
     * @return
     * @throws Exception
     */
    @Override
    public void delGroupTypeByGroupId(Integer groupId) {
        QueryWrapper<GroupType> wrapper = new QueryWrapper<>();
        wrapper.eq("group_id",groupId);
        remove(wrapper);
    }

    /**
     * 通过类别id删除属性组、类别绑定
     * @param catId 类别id
     * @return
     * @throws Exception
     */
    @Override
    public void delGroupTypeByCatId(Integer catId) {
        QueryWrapper<GroupType> wrapper = new QueryWrapper<>();
        wrapper.eq("cat_id",catId);
        remove(wrapper);
    }

    /**
     * 通过id修改属性组、类别绑定
     * @param param 修改类别属性组绑定对象
     * @return 类别属性组绑定dto对象
     * @throws Exception
     */
    @Override
    @GlobalTransactional(rollbackFor = Throwable.class)
    public GroupTypeDto updateGroupTypeById(UpdateGroupTypeParam param) throws Exception {
        GroupType groupType = getById(param.getId());
        if (groupType==null) throw new Exception("所选类别、属性组绑定信息不存在");
        AttrGroupDto group = groupService.findAttrGroupById(param.getGroupId());
        if (group==null||group.getGroupState()!= StateNum.ATTR_GROUP_STATE_NORMAL) throw new Exception("所选属性组存在异常");
        QueryWrapper<GroupType> wrapper = new QueryWrapper<>();
        wrapper.eq("group_id",param.getGroupId());
        wrapper.eq("cat_id",groupType.getCatId());
        GroupType selectOne = getOne(wrapper);
        if (selectOne!=null) throw new Exception("已存在对应类别、属性组绑定信息");
        GroupType bean = BeanUtil.toBean(param, GroupType.class);
        updateById(bean);
        GroupTypeDto dto = BeanUtil.toBean(bean, GroupTypeDto.class);
        dto.setGroupDto(group);
        return dto;
    }

    /**
     * 通过类别id修改属性组、类别绑定
     * @param param 新增类别属性组绑定对象
     * @return 类别属性组绑定dto对象集合
     * @throws Exception
     */
    @Override
    @GlobalTransactional(rollbackFor = Throwable.class)
    public List<GroupTypeDto> updateGroupTypeByCatId(AddGroupTypeParam param) throws Exception {
        remove(new QueryWrapper<GroupType>().eq("cat_id",param.getCatId()));
        return addGroupType(param);
    }

    /**
     * 通过id查看属性组、类别绑定
     * @param id 类别属性组绑定信息id
     * @return 类别属性组绑定dto对象
     * @throws Exception
     */
    @Override
    public GroupTypeDto findGroupTypeById(Integer id) throws Exception {
        GroupType groupType = getById(id);
        AttrGroupDto group = groupService.findAttrGroupById(groupType.getGroupId());
        GroupTypeDto dto = BeanUtil.toBean(groupType, GroupTypeDto.class);
        dto.setGroupDto(group);
        return dto;
    }

    /**
     * 通过属性组id、类别id查看属性组、类别绑定
     * @param subCatId 类别id
     * @param groupId  属性组id
     * @return 类别属性组绑定dto对象
     * @throws Exception
     */
    @Override
    public GroupTypeDto findGroupTypeByCatGroup(Integer subCatId, Integer groupId) throws Exception {
        QueryWrapper<GroupType> wrapper = new QueryWrapper<>();
        wrapper.eq("cat_id",subCatId);
        wrapper.eq("group_id",groupId);
        GroupType groupType = getOne(wrapper);
        if (groupType==null){
            throw new Exception("不存在绑定信息");
        }
        AttrGroupDto group = groupService.findAttrGroupById(groupType.getGroupId());
        GroupTypeDto dto = BeanUtil.toBean(groupType, GroupTypeDto.class);
        dto.setGroupDto(group);
        return dto;
    }

    /**
     * 通过类别id查看属性组、类别绑定
     * @param catId 类别id
     * @return 类别属性组绑定dto对象集合
     * @throws Exception
     */
    @Override
    public List<GroupTypeDto> findGroupTypeByCatId(Integer catId) throws Exception {
        QueryWrapper<GroupType> wrapper = new QueryWrapper<>();
        wrapper.eq("cat_id",catId);
        List<GroupType> types = list(wrapper);
        List<GroupTypeDto> dtos = new ArrayList<>();
        for (GroupType type : types) {
            GroupTypeDto dto = BeanUtil.toBean(type, GroupTypeDto.class);
            AttrGroupDto group = groupService.findAttrGroupById(type.getGroupId());
            group.setId(type.getId());
            dto.setGroupDto(group);
            dtos.add(dto);
        }
        return dtos;
    }

    /**
     * 通过属性组id查看属性组、类别绑定
     * @param groupId 属性组id
     * @return 类别属性组绑定dto对象集合
     * @throws Exception
     */
    @Override
    public List<GroupTypeDto> findGroupTypeByGroupId(Integer groupId) throws Exception {
        QueryWrapper<GroupType> wrapper = new QueryWrapper<>();
        wrapper.eq("group_id",groupId);
        List<GroupType> types = list(wrapper);
        List<GroupTypeDto> dtos = new ArrayList<>();
        for (GroupType type : types) {
            GroupTypeDto dto = BeanUtil.toBean(type, GroupTypeDto.class);
            AttrGroupDto group = groupService.findAttrGroupById(type.getGroupId());
            group.setId(type.getId());
            dto.setGroupDto(group);
            GoodsCat cat = goodsCatService.findCatById(type.getCatId());
            GoodsCatDto catDto = BeanUtil.toBean(cat, GoodsCatDto.class);
            dto.setCatDto(catDto);
            dtos.add(dto);
        }
        return dtos;
    }
}
